package org.lztvn.elearning.front_controllers;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.apache.commons.io.IOUtils;
import org.lztvn.elearning.controllers.AppController;
import org.lztvn.elearning.entities.Product;
import org.lztvn.elearning.entities.User;
import org.lztvn.elearning.service.IUserService;
import org.lztvn.elearning.service.Utility;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

@Controller
@SessionAttributes("userFLogin")
public class ProfileController extends AppController {

	@Autowired
	IUserService service;
	
	Utility util;

	private void init() {
		ApplicationContext context = new ClassPathXmlApplicationContext(
				"applicationContext.xml");
		this.util = (Utility) context.getBean("util");
	}

	@RequestMapping(value = "/profile", method = RequestMethod.GET)
	public ModelAndView profile(Model model, HttpSession session) {
		init();
		if (util.checkFrontSession(session) == false) {
			return new ModelAndView("redirect:lstcourses/login");
		} else {
			User u = (User) session.getAttribute("userFLogin");
			model.addAttribute("user", u);

			return new ModelAndView("profile");
    	}
    }
	
	@RequestMapping(value = "/saveProfile", method = RequestMethod.POST)
	public String saveProfile(@Valid User user, BindingResult bindingResult,
			Model model, MultipartFile file, HttpSession session, HttpServletRequest request) throws IOException {
		
		if (bindingResult.hasErrors()) {
			User user_sess = (User) session.getAttribute("userFLogin");
			model.addAttribute("user", user_sess);
			return ("profile");
		}
		user.setTypeUser(service.getUser(user.getIdUser()).getTypeUser());
		if (!file.isEmpty()) {
			String path = System.getProperty("java.io.tmpdir");
			user.setPhoto(file.getOriginalFilename());
			Long idU = null;
			service.modifierUser(user);
			idU = user.getIdUser();
			
			file.transferTo(new File(path + "/" + "USER_" + idU + "_"
					+ file.getOriginalFilename()));
		} else {
			user.setPhoto(service.getUser(user.getIdUser()).getPhoto());
			service.modifierUser(user);
		}
		request.getSession().setAttribute("userFLogin", user);
		model.addAttribute("success", "Your profile is modified successfully");
		
		return "profile_success";
	}
	
	@RequestMapping(value = "/photoProfile/{idUser}", produces = MediaType.IMAGE_JPEG_VALUE)
	@ResponseBody
	public byte[] photoPro(@PathVariable Long idUser) throws IOException {
		User u = service.getUser(idUser);
		File f = new File(System.getProperty("java.io.tmpdir") + "/USER_" + idUser
				+ "_" + u.getPhoto());
		return IOUtils.toByteArray(new FileInputStream(f));
	}
}
